package com.fangzc.springboot.demo.common.aspect;

import cn.hutool.core.date.DateUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.json.JSONUtil;
import com.fangzc.springboot.demo.common.param.OutVoGlobal;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Copyright (C) 2014-2016 天津紫藤科技有限公司. Co. Ltd. All Rights Reserved.
 *
 * @author fangzc
 * @version v1
 * @description controller层出入参日志
 * @date 2019-5-28
 */
@Aspect
@Component
public class VisitLogAspect {
    private static final Logger LOGGER = LoggerFactory.getLogger(VisitLogAspect.class);

    @Resource
    private HttpServletRequest request;

    @Pointcut("execution(* com.lhg.*.controller..*(..))")
    public void log() {
        LOGGER.info(JSONUtil.toJsonStr(request.getParameterMap()));
    }

    @Before("log()")
    public void before(JoinPoint joinPoint) {
        StringBuilder sb = new StringBuilder("\n----------------------SpringMVC request report start ")
                .append(DateUtil.formatDateTime(new Date()))
                .append(" ------------------------------\n");
        sb.append("ip          : " + ServletUtil.getClientIP(request)).append("\n");
        String uri = request.getRequestURI();
        if (uri != null) {
            sb.append("url         : ").append(uri).append("\n");
        }
        sb.append("parameter   : " + JSONUtil.toJsonPrettyStr(request.getParameterMap()));
        sb.append("\n");
        sb.append("controller  : ").append(joinPoint.getTarget().getClass().getName()).append(".(")
                .append(joinPoint.getTarget().getClass().getSimpleName()).append(".java:1)");
        sb.append("\nmethod      : ").append(joinPoint.getSignature().getName()).append("\n");
        sb.append("----------------------SpringMVC request report end ----------------------------------------------------\n");
        String requestInfo = sb.toString();
        LOGGER.info(requestInfo);
    }

    @AfterReturning(pointcut = "log()", returning = "returnValue")
    public void afterReturning(JoinPoint joinPoint, Object returnValue) {
        if (returnValue != null) {
            StringBuilder sb = new StringBuilder("\n----------------------SpringMVC response report start ").append(DateUtil.formatDateTime(new Date())).append(" ------------------------------\n");
            if (returnValue instanceof OutVoGlobal) {
                sb.append("returnValue : " + JSONUtil.toJsonStr(returnValue)).append("\n");
            }
            sb.append("returnType  : " + returnValue.getClass().getTypeName()).append("\n");
            sb.append("----------------------SpringMVC response report end ----------------------------------------------------\n");
            String responseInfo = sb.toString();
            LOGGER.info(responseInfo);
        }
    }

}